package example;

import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.cfg.Configuration;

public class HibernateUtil {

	private static SessionFactory sessionFactory;

	public static SessionFactory getSessionFactory(boolean annotation) {
		if (sessionFactory == null) {
			if (annotation) {
				sessionFactory = new AnnotationConfiguration().configure()
						.buildSessionFactory();
			} else {
				sessionFactory = new Configuration() // from
														// hibernate.properties
						.configure() // from hibernate.cfg.xml
						.buildSessionFactory();
			}
		}
		return sessionFactory;
	}

	public static SessionFactory getSessionFactory(String cfgFile, boolean annotation) {
		if (sessionFactory == null) {
			if (annotation) {
				sessionFactory = new AnnotationConfiguration().configure(cfgFile)
						.buildSessionFactory();
			} else {
				sessionFactory = new Configuration() // from
														// hibernate.properties
						.configure(cfgFile) // from hibernate.cfg.xml
						.buildSessionFactory();
			}
		}
		return sessionFactory;
	}

	public static void shutdown() {
		sessionFactory.close();
	}

}
